package com.apestech.framework.xml.sql.node;

import java.util.ArrayList;
import java.util.List;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.condition.ConditionMap;
import com.apestech.framework.sql.modul.FromList;
import org.dom4j.Element;

import com.apestech.framework.sql.modul.SqlList;
import com.apestech.framework.sql.variable.SqlSection;
import com.apestech.framework.stream.ArrayObject;
import com.apestech.framework.util.StringUtil;
import org.dom4j.Text;

public class NodeFor extends StringUtil{
	
	private Element node;
	private String item;
	private String index;
	private String collection;
	private String open;
	private String separator;
	private String close;
	private String text;
	private String ntext;
	private SqlList param;
	private FromList fromtab;
	
	public NodeFor(Element node){
		this.node=node;
		this.item=node.attributeValue("item");
		this.index=node.attributeValue("index");
		this.collection=node.attributeValue("collection");
		this.open=node.attributeValue("open");
		this.separator=node.attributeValue("separator");
		this.close=node.attributeValue("close");
		this.ntext=node.getTextTrim();	
	}

	public void setFromtab(FromList fromtab) {
		this.fromtab = fromtab;
	}

	public SqlList getParam(){
		return this.param;
	}
	
	public void doExpress(ConditionMap input) throws Exception{
		List<?> list=this.node.content();
		ArrayObject obj = null;
		this.param=new SqlList();
		Object value=null;
		if(!"id".equals(this.index)){
			obj=new ArrayObject(input.getFieldValue(collection));
		}
		this.text="";
		int inx=0;
		if("id".equals(this.index)){
			SqlSection sqlvar=new SqlSection(fromtab, input);
			ArrayList<String> klist=input.getKeylist();
			int len=klist.size();
			for(int i=0; i<len; i++){
				String s1=this.ntext;
				String key=klist.get(i);
				if(!"".equals(this.ntext)){
					s1=setSeparator(s1, separator, i==0, i==len-1);
				}
				s1=s1+" ";
				sqlvar.doVar(s1);
				this.param.add(sqlvar.getParam());
				this.text=this.text+s1;
			}
		} else {
			while(obj.next()){
				inx++;
				String v=obj.getText();
				String s1=this.ntext;
				if("".equals(s1.trim())){
					int len=list.size();
					for(int i=0; i<len; i++){
						String scalss=list.get(i).getClass().getName();
						if(scalss.equals("org.dom4j.tree.DefaultElement")){
							NodeValue value1=new NodeValue();
							JSONObject map=new JSONObject();
							map.put(collection, v);
							value1.doExpress((Element) list.get(i), new ConditionMap(map));
							String txt=value1.getText();
							if(!"".equals(txt.trim())){
								if(!"".equals(this.text.trim())){
									this.text=this.text+separator;
								}
								this.text=this.text+txt;
							}
							this.param.add(value1.getParam());
						}
					}
				} else {
					s1=s1.replace("item."+collection, "item."+collection+"_"+v);
					input.getSingle().put("item."+collection+"_"+v, v);
					if(!"".equals(this.ntext)){
						s1=setSeparator(s1, separator, inx==1, inx==obj.size());
					}
					s1=s1+" ";
					SqlSection sqlvar=new SqlSection(fromtab, v);
					sqlvar.doVar(s1);
					this.param.add(sqlvar.getParam());
					this.text=this.text+s1;
				}
			}
		}

		if(!"".equals(this.text)){
			this.param.insertFirst(this.open);
			this.text=this.open+this.text+this.close;
			this.param.add(this.close);
		}
	}

	public String getText(){
		return this.text;
	}
	
	/*
    <foreach item="item" index="index" collection="BM01"  open=" AND (" separator=" or " close=")"> 
			BM01 LIKE '${item.BM01}' || '%' 
	</foreach>
	
	<select id="selectUserInfoByForEachArray" resultMap="UserInfoResult">  
        select * from userinfo  
        <if test="array!=null">  
            where userid in  
            <foreach item="item" collection="array" index="index" open="("  
                separator="," close=")">  
                #{item}  
            </foreach>  
        </if>  
    </select>
    
    <select id="selectUserInfoByForEachList" resultMap="UserInfoResult">  
        select * from userinfo  
        <if test="list!=null">  
            where userid in  
            <foreach item="item" collection="list" index="index" open="("  
                separator="," close=")">  
                #{item}  
            </foreach>  
        </if>  
    </select>  
    
    <select id="selectUserInfoByForEach" parameterType="Map"  
        resultMap="UserInfoResult">  
        select * from userinfo  
        <if test="userids!=null">  
            where userid in  
            <foreach item="ParamsId" collection="userids" index="index"  
                open="(" separator="," close=")">  
                #{ParamsId}  
            </foreach>  
        </if>  
    </select>  
    
	<insert id="insertUserByForEachIndex">  
        insert into sysuser (id,password) values  
        <foreach collection="map" index="id" item="item" separator=","  
            open="" close="">  
            (#{id},#{item})  
        </foreach>  
    </insert>   
	 
    */
	
	
}
